package com.carkeeper.user.controller;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.user.pojo.Department;
import com.carkeeper.user.service.DepartmentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    @ResponseBody
    public Result findAll(){
        List<Department> departmentList = departmentService.findAll();
        return new Result(true, StatusCode.OK,"查询部门信息成功",departmentList);
    }
    /***
     * 新增部门
     * @param department
     * @return
     */
    @PostMapping
    @ResponseBody
    public Result add(@RequestBody Department department){
        departmentService.add(department);
        return new Result(true,StatusCode.OK,"新增部门成功");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ResponseBody
    public Result findById(@PathVariable Integer id){
        Department department=departmentService.findById(id);
        System.out.println("department = " + department);
        return new Result(true,StatusCode.OK,"查询成功",department);
    }

    /***
     * 修改数据
     * @param department
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @ResponseBody
    public Result update(@RequestBody Department department,@PathVariable Integer id){
        department.setId(id);
        departmentService.update(department);
        return new Result(true,StatusCode.OK,"修改信息成功");
    }

    /***
     * 根据ID删除部门数据(要改成逻辑删除)
     * @param id
     * @return
     */
    @PutMapping ("/delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable Integer id){

        return  departmentService.deleteById(id);
    }

    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    @ResponseBody
    public Result findPage(@RequestParam Map<String,Object> searchMap,@PathVariable("page") int page, @PathVariable("size") int size){
        Page<Department> pageList =departmentService.findPage(searchMap,page,size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);

    }





}
